<template>
    <div class="admin">
        <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
            <el-button type="primary" @click="handleAdd">
                <el-icon class="mr-1">
                    <Plus />
                </el-icon>
                新增
            </el-button>

            <div class="mt-4">
                <el-table :data="pager.lists" size="large" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
                    <!-- <el-table-column label="ID" prop="id" min-width="60" />> -->

                    <el-table-column label="头像" min-width="100">
                        <template #default="{ row }">
                            <el-avatar :src="row.avatar" :size="50" />
                        </template>
                    </el-table-column>
                    <el-table-column label="账号" prop="username" min-width="100" />
                    <el-table-column label="性别" prop="gender" min-width="60">
                        <!-- <template #default="{ row }">
                        {{ row.gender === "male" ? "男" : row.gender === "female" ? "女" : "未知" }}
                    </template> -->
                        <template #default="{ row }">
                            <el-icon v-if="row.gender === 'male'" color="#409EFF" :size="16">
                                <Male />
                            </el-icon>
                            <el-icon v-else-if="row.gender === 'female'" color="#F56C6C" :size="16">
                                <Female />
                            </el-icon>
                            <el-icon v-else color="#909399" :size="16">
                                <QuestionFilled />
                            </el-icon>
                            <span v-else>未知</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="邮箱" prop="email" min-width="120" />

                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleEdit(row)"> 编辑 </el-button>
                            <el-button v-if="row.root != 1" type="danger" link @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="mt-4 flex justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="admin">
import feedback from "@/utils/feedback";

import EditPopup from "./edit.vue";
import { deleteUserAPI, getUsersAPI } from "@/api/user";
import { usePaging } from "@/hooks/usePaging";
import Pagination from "@/components/pagination/index.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 查询表单数据
const formData = reactive({
    useId: "",
    bookId: "",
    content: ""
});

const selectedIds = ref<number[]>([]);

const showEdit = ref(false);
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: getUsersAPI,
    params: formData
});

const handleAdd = async () => {
    showEdit.value = true;
    await nextTick();
    editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
    showEdit.value = true;
    await nextTick();
    editRef.value?.open("edit");
    editRef.value?.setFormData(data);
};

const handleDelete = async (id: string) => {
    await feedback.confirm("确定要删除？");
    await deleteUserAPI(id);
    getLists();
};

const handleSelectionChange = (selection: any) => {
    selectedIds.value = selection.map((item: any) => item.id);
};

onMounted(() => {
    getLists();
});
</script>
